package com.forlove.app.qselect.friend.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.AppCompatTextView;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.forlove.app.ForloveApplication;
import com.forlove.app.R;
import com.forlove.app.common.base.ui.BaseActivity;
import com.forlove.app.common.utils.LogUtil;
import com.forlove.app.common.widget.CircleImageView;
import com.forlove.app.event.EventEntity;
import com.forlove.app.manager.EventManager;
import com.forlove.app.manager.NetworkManager;
import com.forlove.app.manager.ServerManager;
import com.forlove.app.manager.UserManager;
import com.forlove.app.qselect.CommonEntity;
import com.forlove.app.qselect.answer.entity.AnswerEntity;
import com.forlove.app.qselect.answer.ui.AnswerSubjectActivity;
import com.forlove.app.qselect.answer.widght.InviteFriendDialog;
import com.forlove.app.qselect.friend.entity.MineFriendEntity;
import com.forlove.app.qselect.friend.entity.SearchFriendEntity;
import com.forlove.app.qselect.friend.presenter.SearchUserinfoPresenter;
import com.forlove.app.qselect.friend.view.SearchUserinfoView;
import com.forlove.app.qselect.login.entity.UserEntity;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;

import static com.forlove.app.qselect.friend.ui.MineNewFriendFragment.add_success;

/**
 * 搜索好友后的用户资料 添加好友申请页面
 * Created by yjl on 2017/10/14.
 */

public class SearchUserInfoActivity extends BaseActivity implements View.OnClickListener, SearchUserinfoView {

    @BindView(R.id.bt_invite_friend)
    AppCompatTextView bt_invite_friend;

    @BindView(R.id.view_fade_back)
    FrameLayout view_fade_back;

    @BindView(R.id.view_friend_photo)
    CircleImageView view_friend_photo;

    @BindView(R.id.text_friend_account)
    TextView text_friend_account;

    @BindView(R.id.image_friend_sex)
    ImageView image_friend_sex; //性别

    @BindView(R.id.text_friend_hot_num)
    TextView text_friend_hot_num;

    @BindView(R.id.view_line)
    View view_line;

    @BindView(R.id.text_friend_province)
    TextView text_friend_province;  //省份

    @BindView(R.id.text_friend_city)
    TextView text_friend_city;//城市

    @BindView(R.id.text_tip_unregister)
    TextView text_tip_unregister;

    @BindView(R.id.view_send_request)
    LinearLayout view_send_request;

    @BindView(R.id.edittext_validate)
    AppCompatEditText edittext_validate; //好友验证信息

//    private InviteFriendDialog inviteFriendDialog;

    @Override
    public int getLayoutId() {
        return R.layout.activity_search_user_info;
    }

    private SearchFriendEntity searchFriendEntity;
    private SearchUserinfoPresenter searchUserinfoPresenter;
    private MineFriendEntity friendEntity;
    private String addMsg;//好友申请验证
    private String from;

    @Override
    public void initView() {
        searchUserinfoPresenter = new SearchUserinfoPresenter(this);
        setPresenter(searchUserinfoPresenter);
//        inviteFriendDialog = new InviteFriendDialog(this, "", "");
        bt_invite_friend.setOnClickListener(this);
        view_fade_back.setOnClickListener(this);
        initData();
    }

    private void initData() {
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            searchFriendEntity = (SearchFriendEntity) bundle.getSerializable("friendinfo");
            from = bundle.getString("from");
            LogUtil.e("yjl", "from:" + from);
            String mobile = bundle.getString("mobile");
            requestSearchFriend(mobile);
        }
        if (searchFriendEntity != null) {
            showView(searchFriendEntity);
        }
    }

    private void showView(SearchFriendEntity entity) {
        friendEntity = entity.friendEntity;
        if (friendEntity == null) {
            return;
        }
        searchUserinfoPresenter.loadImageView(this, view_friend_photo,
                friendEntity.img, R.mipmap.image_mine_friend_avatar);
        if (friendEntity != null && entity.note_times == 0) {
            text_friend_hot_num.setText(friendEntity.charm + "");
        } else {
            text_friend_hot_num.setText(entity.note_times + "");
        }
        if (friendEntity.user_id == 0) {
            showUnRegisterFriendView(friendEntity);
        } else if (friendEntity.user_id > 0) {
            showRegisterView(friendEntity);
        }
    }

    private void showRegisterView(MineFriendEntity entity) {

        text_friend_account.setText(entity.getAvaliableName());
        if (entity.sex == 1) {
            image_friend_sex.setImageResource(R.mipmap.image_sex_boy);
        } else if (entity.sex == 2) {
            image_friend_sex.setImageResource(R.mipmap.image_sex_girl);
        }
        image_friend_sex.setVisibility(View.VISIBLE);

        view_line.setVisibility(View.VISIBLE);
        text_tip_unregister.setVisibility(View.INVISIBLE);
        view_send_request.setVisibility(View.VISIBLE);
        bt_invite_friend.setText("发送验证");
        //TODO 暂时注掉省会信息
        view_line.setVisibility(View.INVISIBLE);
        text_friend_province.setVisibility(View.INVISIBLE);
        text_friend_city.setVisibility(View.INVISIBLE);
    }

    private void showUnRegisterFriendView(MineFriendEntity friendEntity) {
        text_friend_account.setText(friendEntity.getAvaliableName());
        image_friend_sex.setVisibility(View.INVISIBLE);
        text_friend_hot_num.setText(searchFriendEntity.note_times + "");
        view_line.setVisibility(View.INVISIBLE);
        text_tip_unregister.setVisibility(View.VISIBLE);
        view_send_request.setVisibility(View.GONE);
        bt_invite_friend.setText("邀请好友");
        //TODO 暂时注释掉省会信息
        text_friend_province.setVisibility(View.INVISIBLE);
        text_friend_city.setVisibility(View.INVISIBLE);
    }

    private void requestSendAdd() {
        if (friendEntity == null) {
            return;
        }
        addMsg = edittext_validate.getText().toString();
        Map param = new HashMap();
        param.put("msg", addMsg);
        param.put("friend", friendEntity.mobile);
        searchUserinfoPresenter.loadSendRequestData(param);
    }

    private void requestSearchFriend(String mobile) {
        if (TextUtils.isEmpty(mobile)) {
            return;
        }
        Map param = new HashMap();
        param.put("mobile", mobile);
        searchUserinfoPresenter.loadSearchFriend(param);
    }

    @Override
    protected void onResume() {
        super.onResume();
//        StatusBarCompat.compat(this, Color.parseColor("#ffffff"));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_invite_friend:
                if (friendEntity != null && friendEntity.user_id == 0) {
//                    Map param = new HashMap();
//                    if (friendEntity != null && !TextUtils.isEmpty(friendEntity.contactsNumber)) {
//                        param.put("mobile", friendEntity.contactsNumber);
//                    } else if (friendEntity != null && !TextUtils.isEmpty(friendEntity.mobile)) {
//                        param.put("mobile", friendEntity.contactsNumber);
//                    }
//                    searchUserinfoPresenter.loadInviteFriendBySms(param);
                    UserEntity userEntity = UserManager.getUesrInfo(ForloveApplication.getInstance());
                    String commonUrl = ServerManager.getServerUrl() + "/invtepage?username=" + userEntity.userName;
                    InviteFriendDialog dialog = new InviteFriendDialog(this, commonUrl, null, null);
                    dialog.show();
                } else {
                    // 添加好友申请
                    requestSendAdd();
                }

                break;
            case R.id.view_fade_back:
                onBackPressed();
                break;
        }
    }


    @Override
    public void showErrorData(NetworkManager.ResultCode resultCode, String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void sendAddRequestSuccess(NetworkManager.ResultCode resultCode, String msg) {
        //添加好友成功
        Toast.makeText(this, "好友申请发送成功!", Toast.LENGTH_SHORT).show();
//        EventEntity eventEntity = new EventEntity();
//        eventEntity.eventId = EventManager.EVENT_ADD_NEW_FRIEND;
//        EventBus.getDefault().post(eventEntity);


        setResult(add_success, getIntent());
        //返回上一页
        finish();
    }

    @Override
    public void showInviteFriendView(CommonEntity commonEntity) {
        Toast.makeText(this, "邀请成功，您的好友将收到邀请短信", Toast.LENGTH_SHORT).show();
        if (!TextUtils.isEmpty(from) && from.equals("LimitAnswerActivity")) {
            //如果从答题限制页过来则返回答题
            searchUserinfoPresenter.loadResetQuestionData(new HashMap());
//            startActivity(new Intent(this, AnswerSubjectActivity.class));
            EventEntity eventEntity = new EventEntity();
            eventEntity.eventId = EventManager.EVENT_INVITE_CONTACTS;
            EventBus.getDefault().post(eventEntity);
        } else {
            EventEntity eventEntity = new EventEntity();
            eventEntity.eventId = EventManager.EVENT_MINE_CONTACTS;
            EventBus.getDefault().post(eventEntity);
            finish();
        }
    }

    @Override
    public void showResetAnswerView(AnswerEntity answerEntity) {
        Intent intent = new Intent(this, AnswerSubjectActivity.class);
        Bundle bundle = new Bundle();
        if (answerEntity != null) {
            bundle.putSerializable("answer", answerEntity.serializeToString());
        }
        intent.putExtras(bundle);
        startActivity(intent);
        this.finish();
    }

    @Override
    public void showSearchViewData(SearchFriendEntity searchFriendEntity) {
        showView(searchFriendEntity);
    }
}
